﻿@inherits BaseTablePage<CmCategory>

<div class="kui-table" style="height:100%;">
    <KToolbar>
        <KTitle Text="@PageName" />
        <Toolbar Model="Table.Toolbar" />
    </KToolbar>
    <CategoryTypeTable Context="Context" Model="Table" DefaultExpandAllRows DefaultExpandMaxLevel="1"
                       @bind-SelectedRows="Table.SelectedRows">
        <Selection Type="checkbox" Align="ColumnAlign.Center" Width="50" Class="kui-table-check" />
        <StringColumn Title="代码" Width="100" @bind-Field="@context.Code" />
        <StringColumn Title="名称" Width="200" @bind-Field="@context.Name">
            <Icon Type="@context.Icon" Theme="outline" />
            <span style="margin-left:5px;">@context.Name</span>
        </StringColumn>
        <IntegerColumn Title="顺序" Width="100" @bind-Field="@context.Sort" />
        <BooleanColumn Title="启用" Width="100" @bind-Field="@context.Enabled">
            <Switch Disabled Checked="context.Enabled" />
        </BooleanColumn>
        <StringColumn Title="备注" @bind-Field="@context.Note" />
        <ActionColumn Title="操作" Width="200" Class="kui-table-action">
            <Tag Color="blue-inverse" OnClick="e=>Edit(context)">编辑</Tag>
            <Tag Color="red-inverse" OnClick="e=>Delete(context)">删除</Tag>
            <Tag Color="" OnClick="e=>Add(context)">添加下级</Tag>
        </ActionColumn>
    </CategoryTypeTable>
</div>

@code {
    protected virtual ContentType Type { get; }

    private ICommonService Service;

    protected override async Task OnPageInitAsync()
    {
        await base.OnPageInitAsync();
        Service = await CreateServiceAsync<ICommonService>();
        Table.TreeChildren = item => item.Children;
        Table.FormTitle = row => $"{PageName} - {row.Parent?.FullName}";
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            await RefreshAsync();
        }
    }

    public override async Task RefreshAsync()
    {
        Table.DataSource = await GetTreeDataSourceAsync();
        await StateChangedAsync();
    }

    public void New() => Add(new CmCategory { Id = "0" });
    public void DeleteM() => Table.DeleteM(Service.DeleteCategoriesAsync);

    private void Add(CmCategory row)
    {
        var model = new CmCategory
        {
            Type = Type.ToString(),
            ParentId = row.Id,
            Enabled = true
        };
        Table.NewForm(Service.SaveCategoryAsync, model);
    }

    private void Edit(CmCategory row)
    {
        row.Parent = null;
        row.Children = null;
        Table.EditForm(Service.SaveCategoryAsync, row);
    }

    private void Delete(CmCategory row)
    {
        var model = new CmCategory { Id = row.Id, Name = row.Name };
        Table.Delete(Service.DeleteCategoriesAsync, model);
    }

    private async Task<List<CmCategory>> GetTreeDataSourceAsync()
    {
        var lists = await Service.GetCategoriesAsync(Type.ToString());
        if (lists == null || lists.Count == 0)
            return [];

        return lists.ToTreeData();
    }
}